#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
use strict;
use CGI qw(param);

# Startupscript casetool, part of Sipman.

my $module = $ARGV[0];
my $tool = $ARGV[1];
my $action = $ARGV[2];
my $option1 = $ARGV[3];
my $option2 = $ARGV[4];
my $option3 = $ARGV[5];


my $backHeader="black";
my $httpUser = $ENV{'REMOTE_USER'};
my $httpReF = $ENV{'HTTP_REFERER'};
my $httpScriptName = $ENV{'SCRIPT_NAME'};
my $httpUserIp = $ENV{'REMOTE_ADDR'};
my $httpServerName = $ENV{'HTTP_HOST'};
my $httpCookie = $ENV{'HTTP_COOKIE'};
my $httpHost = $ENV{'HTTP_HOST'};
my $fixedInfoDir = $ENV{'SIM_FIXEDINFOD'};

if ( ! -d $fixedInfoDir ) {
  print("Oops, no direcotry found to store the fixed info, exiting.....");
  exit;
}

sub catFile {
  my $output = "";
  my ($file) = @_;
  open FH, $file or $output = "none";
# $output = readline FH;
  $output = <FH>;
  close FH;
  return $output;
}

sub catFileLine {
  my $outPut = "";
  my ($file) = @_;
  open FH, $file or $outPut = "$file";
  $outPut = <FH>;
  close FH;
  chomp($outPut);  # remove the newline
  return $outPut;
}

sub printFileLine {
  my $file = "@_[0]";
  my $varName = "@_[1]";
  my $rest = "@_[2]";
  my $logFile = "@_[3]";
  my $oldVal = catFileLine ("$file");
# print ("Update file=$file, varName=$varName, content:$rest<BR>");
  if ( "$oldVal" ne "$rest" ) {
    open(OUT, ">$file") || die "cannot create $file: $! (file=$file, varName=$varName, rest=$rest)";
    if ( "$logFile" ne "quiet" ) {
      print ("$varName is gewijzigd, oud:$oldVal nieuw:$rest<BR>");
    }
    print OUT $rest;
    close(OUT);
  }
}

sub testNameStrict {
  my $testname = "@_[0]";
  my $type = "@_[1]";
  if ( "$type" eq "word" ) {
    $testname =~ tr#a-zA-Z[\xE0][\xE8][\xEC][\xF2][\xF9]##cd;
  } else {
    $testname =~ tr#a-zA-Z0-9[\@][\.][\-]#A#cds;
  }
  #print ("testname=$testname");
  return $testname;
}

sub testMkDir {
  my $dirName = "@_[0]";
  if ( !-d $dirName ) {
    system "mkdir -p $dirName";
  }
}

sub printMainMenu {
  my $fontColor = "#222222";
  print("<CENTER>
    <A HREF=$httpScriptName?$module+$tool><FONT COLOR=$fontColor>list</FONT></A> |
    <A HREF=$httpScriptName?$module+$tool+add><FONT COLOR=$fontColor>add</FONT></A>
    </TD></TR></TABLE><CENTER>
        ");
}

sub listFiles {
  chdir( $fixedInfoDir );
  my @fileList = <*>;
  foreach my $fileName (@fileList) {
    print("
      <A HREF=$httpScriptName?$module+$tool+updFile+$fileName>$fileName</A><BR>
          ");
  }
}

sub updFile {
  my $fileName = "@_[0]";
  my $noteFile = "$fixedInfoDir/$fileName";
  if ( "$fileName" eq "none" ) {
    $fileName = param("fileName");
  }
  if ( "$fileName" eq "" ) {
    print("
       <FORM method=post Action=$httpScriptName?$module+$tool+updFile>
         File name: <INPUT NAME=fileName SIZE=20>
         <INPUT TYPE=submit Value=\"Add\">
       </FORM>
          ");
  } else {
    my $note = param("note");
    $note =~ s/\t/ /g;
    if ( "$note" ne "" ) {
      printFileLine("$noteFile", "note-$fileName","$note","quiet"); 
    }
    if ( -f "$noteFile" ) {
      $note = catFileLine("$noteFile");
    }
    print("
       Updating info for $fileName.<HR>
       <FORM method=post Action=$httpScriptName?$module+$tool+updFile+$fileName>
         <TEXTAREA NAME=\"note\" ROWS=20 COLS=100>");
    if ( -f "$noteFile" ) {
      system(" cat $noteFile ");
    }
    print("</TEXTAREA><BR>
          <INPUT TYPE=submit Value=\"Add / Update\">
       </FORM>
          ");
  }
}

# ------------------------------------- Main ---------------------------------------

&printMainMenu;
print("<CENTER>");
if ( "$action" eq "none" ) {
  listFiles;
} else {
  updFile( $option1 );
}
print("</TABLE>");
#&printFooter;
